1. Technical Field
The present invention relates in general to a system and method for identifying memory leaks. More particularly, the present invention relates to a system and method for tracking memory allocations and memory de-allocations using a minimal amount of system resources.
2. Description of the Related Art
Computer programs request memory space upon start-up and during operation. Some applications request a large memory space in a single request while other applications request multiple, smaller amounts of memory during operation. Some applications require more memory to operate than others. For example, applications with graphic display functions typically require more memory than text-only based applications.
When an operating system receives a memory request, a memory manager determines if the requested memory size is available and where within the memory area the memory is available. If the memory is available, the memory manager sends the application a pointer, or handle, corresponding to the address location of the memory allocated to the application.
If the memory is not available, the memory manager sends a response to the application denying the request. The memory manager does not permit other applications to use allocated memory space assigned to other applications.
When an application is finished using memory, it notifies the memory manager and the memory manager indicates that the memory space is available for use by other applications. For example, an application may send a de-allocation request when the application terminates or when the memory is no longer needed by the application.
Some applications do not inform the memory manager to de-allocate memory when the application is through using the memory. The lack of properly de-allocating memory is called a xe2x80x9cmemory leakxe2x80x9d, or xe2x80x9cresource leakxe2x80x9d. Memory leaks may degrade system performance due to the seeming unavailability of memory space.
Memory leak detection programs are available that track memory leaks. A challenge found with these programs is that they often require a significant amount of memory to operate. This memory usage contradicts the objective of efficiently using a system""s memory space. In addition, traditional memory leak detection programs may not work properly or efficiently due to the amount of system resources required by the memory leak detection program.
In essence, traditional memory leak detection programs are challenged by the fact that they compete for system resources, such as memory and CPU time, with other applications running on the system. What is needed, therefore, is a way to allow the user to easily identify memory leaks during operation using a minimal amount of system resources. This information can then be used to correct identified memory leaks or, if source code is unavailable, to install comparable applications that manage memory more efficiently.
It has been discovered that memory leak detection is achieved using a minimal amount of system resources by setting a memory leak flag which informs the system to initialize a memory array and store memory requests in memory array slots within the memory array.
A user sets a memory leak flag to notify the system to track memory allocations and de-allocations. The user may set the memory leak flag independently or the user may write a script that is run during start-up that sets the memory leak flag.
The operating system generates and initializes a memory array which includes multiple memory array slots. Each memory array slot includes a free flag, an allocation pointer field, and other fields to store information corresponding to a memory allocation. The free flag identifies whether memory allocation information is stored in the corresponding memory array slot. The free flag is set to false for an allocated memory array slot (i.e. memory allocation information is stored in the memory array slot). The allocation pointer corresponds to the memory location of the memory allocation.
When an application no longer requires the allocated memory, the application sends a de-allocation request to the operating system. The request includes a de-allocation pointer which corresponds to the memory location of the allocated memory. The operating system locates the allocated memory array slot corresponding to the de-allocation pointer and sets the corresponding free flag to true, signifying a proper memory de-allocation.
Upon shutdown, the operating system analyzes each free flag in the memory array. When the operating system locates a free flag that is set to false, the operating system retrieves the information in the corresponding allocated memory array slot. The memory array slot includes information corresponding to a memory allocation that has not been properly de-allocated (i.e. a memory leak). After each free flag is analyzed, the operating system generates a report which includes information, such as the application name, corresponding to each memory leak detected.
In a graphical user interface (GUI) environment, such as X-Windows running on a UNIX-based operating system, the memory leak technology can be used to detect memory leaks within the graphical environment components. In this embodiment, an environment variable is set before invoking X-Windows from the UNIX operating system. The environment variable turns memory leak tracing on within the X-Windows environment. X-Windows components process requests from applications and perform other operations to maintain the graphical environment. When memory leak tracing is activated, the a memory leak detector interfaces with the X-Windows memory manager to track components"" use of memory. The memory leak detector stores information pertaining to components"" use of memory in a memory usage array. When X-Windows is shutdown, or when a memory usage report is requested, a report is generated indicating which components have not released memory. In this manner, X-Windows system testers can determine which X-Windows components are not properly releasing memory. These components can then be modified so that memory is properly released when the component is finished using it.
The foregoing is a summary and thus contains, by necessity, simplifications, generalizations, and omissions of detail; consequently, those skilled in the art will appreciate that the summary is illustrative only and is not intended to be in any way limiting. Other aspects, inventive features, and advantages of the present invention, as defined solely by the claims, will become apparent in the non-limiting detailed description set forth below.